using System;
using System.Collections.ObjectModel;
using mb;
using Xamarin.Forms;


namespace libhot
{
	public class TestListPage : ContentPage
	{
		public TestListPage()
		{
			var hrm = CatLib.App.Make<HotReloadManager>();
			hrm.RegisterReloadWeak(this, "calist", (self, resName) =>
			{
				var me = self as TestListPage;
				me.Content = null;
				me.LoadXamlFromRes(resName);
				this.DoLoad();
			});
		}

		ListView listView;
		ObservableCollection<string> items;
		class DataVM
		{
			public ObservableCollection<string> items { get; set; }
		}
		protected void DoLoad()
		{

			items = new ObservableCollection<string> { "alpha", "beta", "gamma", "delta", "epsilon" };
			listView = this.FindByName<ListView>("listView");
			listView.SetBinding(ListView.ItemsSourceProperty, new Binding("items"));
			listView.BindingContext = new DataVM() { items = items };
		}

		public void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
		{
			if (e.SelectedItem == null) return; // has been set to null, do not 'process' tapped event
			DisplayAlert("Tapped", e.SelectedItem + " row was tapped", "OK");
			((ListView)sender).SelectedItem = null; // de-select the row
		}

		public void OnMore(object sender, EventArgs e)
		{
			var mi = ((MenuItem)sender);
			DisplayAlert("More Context Action", mi.CommandParameter + " more context action", "OK");
		}

		public void OnDelete(object sender, EventArgs e)
		{
			var mi = ((MenuItem)sender);
			//DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK");

			App.logger.Log("delete " + mi.CommandParameter.ToString());
			items.Remove(mi.CommandParameter.ToString());
		}
	}
}